package com.example.myblog_ssm.config;

import com.example.myblog_ssm.common.AppVariable;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-03-21
 * Time: 8:48
 */

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * true -> 用户登录
     * false ->用户未登录
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        //已登录 放行
        if (session != null && session.getAttribute(AppVariable.USER_SESSION_KEY) != null) {
            return true;
        }
        //未登录
        response.sendRedirect("/login.html");
        return false;
    }
}
